﻿
using System.Collections.Generic;
using NHibernate;


namespace ProyectoSelva.Daos
{
    public class BaseDAO<T>
    {

        public static ISessionFactory CreateSessionFactory()
        {

            return SessionFactoryHelper.GetSessionFactory();
        }


        public T GetById(int id, bool shouldLock)
        {
            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    T entity;

                    if (shouldLock)
                    {
                        entity = (T)session.Load(typeof(T), id, LockMode.Upgrade);
                    }

                    else
                    {
                        entity = (T)session.Load(typeof(T), id);
                    }
                    return entity;
                }
            }
        }

        public IList<T> ListarTodos()
        {
            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {         
                    return session
                           .CreateCriteria(typeof(T))
                           .List<T>();
                }
            }
        }

        public T Merge(T obj)
        {

            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session
                    .Merge(obj);
                    transaction.Commit();
                }
                return obj;
            }

        }

        public T Insert(T obj)
        {

            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session
                    .Save(obj);

                    transaction.Commit();
                }
                return obj;
            }

        }

        public T Update(T obj)
        {
            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {

                    session
                    .SaveOrUpdate(obj);
                    

                    transaction.Commit();
                }
                return obj;
            }
        }



        public void Delete(T obj)
        {
            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session
                    .Delete(obj);

                    transaction.Commit();
                }
            }

        }

        public T Refresh(T obj)
        {
            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {

                    session
                    .Refresh(obj);


                    transaction.Commit();
                }
                return obj;
            }
        }

    }


}
